//
//  ZLWrongParsingViewController.m
//  ZLExamination
//
//  Created by Zheng on 2017/9/13.
//  Copyright © 2017年 Zheng. All rights reserved.
//

#import "ZLWrongParsingViewController.h"
#import "ZLWrongParsingHeaderView.h"
#import "ZLWrongParsingFooterView.h"
#import "ZLWrongParsingCell.h"

#import "ZLExamResultModel.h"

#import "ZLWrongParsingSheetViewController.h"
#import "ZLBaseNavigationController.h"


@interface ZLWrongParsingViewController () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) ZLWrongParsingHeaderView *headerView;
@property (nonatomic, strong) ZLWrongParsingFooterView *footerView;
@property (nonatomic, strong) UICollectionView *bgCollectionView;

@property (nonatomic) NSUInteger pageNumber; //当前页数
@property (nonatomic) NSUInteger totalPageNumber; //总页数

@property (nonatomic, copy) NSArray<NSArray *> *totalQuestions; //单选多选 总
@property (nonatomic, copy) NSArray<ZLExamResultListModel *> *multiples;
@property (nonatomic, copy) NSArray<ZLExamResultListModel *> *singles; //答案集合
@property (nonatomic, copy) NSArray<NSString *> *questionTitles; //单选项

@end

@implementation ZLWrongParsingViewController

#pragma mark - 点击事件
- (void)myClickAction {
    __weak typeof(self) weakSelf = self;
    
#pragma mark 上翻页操作
    [_footerView setLeftClick:^{
        kCX_LOG(@"向上");
        if (_pageNumber > 1) {
            weakSelf.pageNumber--;
            [weakSelf setDisplayPageNumber];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_pageNumber-1 inSection:0];
            [weakSelf.bgCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        }
    }];
    
#pragma mark 下翻页操作
    [_footerView setRightClick:^{
        kCX_LOG(@"向下");
        if (_pageNumber < _totalPageNumber) {
            weakSelf.pageNumber++;
            [weakSelf setDisplayPageNumber];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:_pageNumber-1 inSection:0];
            [weakSelf.bgCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
        }
        
    }];
#pragma mark 答题卡操作
    [_footerView setSheetClick:^{
        kCX_LOG(@"答题卡");
        
        ZLWrongParsingSheetViewController *ebsVC = [[ZLWrongParsingSheetViewController alloc] init];
        ebsVC.questions = weakSelf.totalQuestions;
        
        ebsVC.clickCallBack = ^(NSInteger number) {
#pragma mark 答题卡选择题号
            [weakSelf scroolToDesignationRowWith:number];
        };
        
        ZLBaseNavigationController *nav = [[ZLBaseNavigationController alloc] initWithRootViewController:ebsVC];
        [weakSelf presentViewController:nav animated:YES completion:^{
            
        }];
    }];
    
}

//- (void)backBarButtonPressed:(id)sender {
//#pragma mark 返回按钮
//    kCX_LOG(@"跳转返回最上级");
//}

- (void)scroolToDesignationRowWith:(NSInteger)rowNumber {
#pragma mark 跳转到固定题号的位置
    _pageNumber = rowNumber+1;
    [self setDisplayPageNumber];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowNumber inSection:0];
    [_bgCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
};


#pragma mark - scrollview delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger index = (NSInteger)scrollView.contentOffset.x/(NSInteger)kSCREEN_WIDTH+1;
    if (_pageNumber != index) {
        _pageNumber = index;
        [self setDisplayPageNumber];
    }
}

- (void)setDisplayPageNumber {
    
    ZLExamResultListModel *model;
    
    if (_pageNumber-1 < _singles.count) {
        //单选
        model = _singles[_pageNumber-1];
    } else {
        //多选
        if (_multiples.count > 0) {
            model = _multiples[_pageNumber-1-_singles.count];
        }
    }
    
#pragma mark - 设置显示页数
    _headerView.titleLbl.text = _pageNumber>_singles.count? @"多项选择题":@"单项选择题";
    NSString *selectedNumber = [NSString stringWithFormat:@"%lu",_pageNumber];
    NSString *totalNumber = [NSString stringWithFormat:@"%lu",_totalPageNumber];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@/%@",selectedNumber,totalNumber]];
    NSRange range = NSMakeRange([selectedNumber length], [totalNumber length]+1);
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:range];
    _headerView.numberLbl.attributedText = str;
    _headerView.pageNumber = _pageNumber;
    
    __weak typeof(self) weakSelf = self;
#pragma mark - 收藏事件
    _headerView.collectCallBack = ^(NSInteger indexNumber, UIButton *sender) {
        
        if (sender.selected) {
            //未收藏 完成收藏
            [[YCRequestDataService sharedManager] Post:@"user/collection/create" parameters:@{@"questions_id":model.questions_id,@"curriculum_id":weakSelf.curriculum_id} success:^(id response) {
                if (response[@"error_code"]) {
                    NSLog(@"%@",response[@"error_msg"]);
                } else {
                    NSLog(@"新增收藏成功");
                    sender.selected = !sender.selected;
                }
            } failure:^{
                NSLog(@"网络或服务器出错");
            }];
        } else {
            //已收藏 取消收藏
            [[YCRequestDataService sharedManager] Post:@"user/collection/delete" parameters:@{@"questions_id":model.questions_id,@"curriculum_id":weakSelf.curriculum_id} success:^(id response) {
                if (response[@"error_code"]) {
                    NSLog(@"%@",response[@"error_msg"]);
                } else {
                    NSLog(@"删除收藏成功");
                    sender.selected = !sender.selected;
                }
            } failure:^{
                NSLog(@"网络或服务器出错");
            }];
        }
    };
}




#pragma mark - 视图生命周期
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _singles = [_answersDataArr[0] copy];
    _multiples = [_answersDataArr[1] copy];
    _totalPageNumber = _singles.count + _multiples.count;
    _pageNumber = 1;

    _questionTitles = @[@"A",@"B",@"C",@"D"];
    
    //总数据
    _totalQuestions = @[_singles,_multiples];
    
    //子视图
    [self subviewsInView];
    
    //设置显示页数
    [self setDisplayPageNumber];
    
    //用户点击事件
    [self myClickAction];
    
}


#pragma mark - 子视图
- (void)subviewsInView {
    
//    [self showBackButtonWithImage:@"exam_back"];
        
    //顶部视图
    [self.view addSubview:self.headerView];
    
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(35);
    }];
    
    //底部视图
    [self.view addSubview:self.footerView];
    
    [_footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.left.right.equalTo(self.view);
        make.height.mas_equalTo(75);
    }];
    
    //答题视图
    [self.view addSubview:self.bgCollectionView];
    
    [_bgCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(_headerView.mas_bottom);
        make.bottom.equalTo(_footerView.mas_top);
    }];
}

#pragma mark - lazy load
- (ZLWrongParsingHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[ZLWrongParsingHeaderView alloc] init];
    }
    return _headerView;
}

- (ZLWrongParsingFooterView *)footerView {
    if (!_footerView) {
        _footerView = [[ZLWrongParsingFooterView alloc] init];
    }
    return _footerView;
}

- (UICollectionView *)bgCollectionView {
    if (!_bgCollectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(kSCREEN_WIDTH, kSCREENH_HEIGHT-110-64);
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _bgCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _bgCollectionView.delegate = self;
        _bgCollectionView.dataSource = self;
        _bgCollectionView.pagingEnabled = YES;
        _bgCollectionView.bounces = NO;
        _bgCollectionView.showsVerticalScrollIndicator = NO;
        _bgCollectionView.showsHorizontalScrollIndicator = NO;
        _bgCollectionView.backgroundColor = [UIColor whiteColor];
        [_bgCollectionView registerClass:[ZLWrongParsingCell class] forCellWithReuseIdentifier:kZLWrongParsingCell_ID];
        [_bgCollectionView registerClass:[ZLWrongParsingCell class] forCellWithReuseIdentifier:kZLWrongParsingMultipleCell_ID];
    }
    return _bgCollectionView;
}

#pragma mark - collectionview datasource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _totalPageNumber;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
            
    if (indexPath.item < _singles.count) {
        //单选
        ZLWrongParsingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kZLWrongParsingCell_ID forIndexPath:indexPath];
        cell.model = _singles[indexPath.item];
        [cell.myTableView reloadData];
        return cell;
        
    } else {
        //多选
        ZLWrongParsingCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kZLWrongParsingMultipleCell_ID forIndexPath:indexPath];
        cell.model = _multiples[indexPath.item-_singles.count];
        [cell.myTableView reloadData];
        return cell;
    }

}

@end
